//
//  ICShareBaseView.m
//  ICome
//
//  Created by ENN on 16/8/31.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICShareBaseView.h"
#import "ICHeaderFile.h"

@interface ICShareBaseView ()
@end

@implementation ICShareBaseView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5.0;
        
        [self.cancelBtn addTarget:self action:@selector(cancelBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.confirmBtn addTarget:self action:@selector(confirmBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)setShareData:(ICShareInstance *)shareData
{
    _shareData = shareData;
    
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:[ICTools getMinImageURL:(shareData.group.photoId)]] placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"App_personal_headimg"]];
    self.nameLabel.text = [ICMessageDatabase getGroupName:shareData.group.gId];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

- (void)cancelBtnClicked
{
    [[NSNotificationCenter defaultCenter] postNotificationName:ICShareCancelNotification object:nil];
}

// 确认分享
- (void)confirmBtnClicked
{
    self.shareData.textFieldStr = self.textField.text;
    [[NSNotificationCenter defaultCenter] postNotificationName:ICShareConfirmNotification object:self.shareData];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.topLabel.frame = CGRectMake(20, 20, 100, 20);
    [self.topLabel sizeToFit];
    self.imageV.frame   = CGRectMake(20, self.topLabel.bottom+15, 60, 60);
    self.nameLabel.frame = CGRectMake(self.imageV.right+10, self.topLabel.bottom+20, 100, 20);
    self.nameLabel.centerY = self.imageV.centerY;
}

- (void)bottomViewFrameWithView:(UIView *)view
{
    self.textField.frame = CGRectMake(20, view.bottom+10, self.width-40, 39);
    self.lineView1.frame = CGRectMake(20, self.textField.bottom, self.textField.width, 0.5);
    self.lineView2.frame = CGRectMake(0, self.lineView1.bottom + 10, self.width, 0.5);
    CGFloat height = self.height - self.lineView2.bottom;
    self.cancelBtn.frame = CGRectMake(0,self.lineView2.bottom, self.width*0.5-1, height);
    self.lineView3.frame = CGRectMake(self.cancelBtn.right, self.lineView2.bottom, 0.5, height);
    self.confirmBtn.frame = CGRectMake(self.lineView3.right, self.cancelBtn.top, self.width-self.cancelBtn.width-self.lineView3.width, height);
}


#pragma mark - Getter

- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"取消" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:btn];
        _cancelBtn = btn;
    }
    return _cancelBtn;
}

- (UIButton *)confirmBtn
{
    if (!_confirmBtn) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"确定" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:btn];
        _confirmBtn = btn;
    }
    return _confirmBtn;
}

- (UIView *)lineView2
{
    if (!_lineView2) {
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = ICRGB(0xc4c4c4);
        [self addSubview:line];
        _lineView2 = line;
    }
    return _lineView2;
}


- (UIView *)lineView3
{
    if (!_lineView3) {
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = ICRGB(0xc4c4c4);
        [self addSubview:line];
        _lineView3 = line;
    }
    return _lineView3;
}

- (UITextField *)textField
{
    if (!_textField) {
        UITextField *textF = [[UITextField alloc] init];
        textF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"给朋友留言" attributes:@{NSForegroundColorAttributeName:ICRGB(0xd1d1d1),NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];
        [self addSubview:textF];
        _textField = textF;
    }
    return _textField;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:17.0];
        label.textColor = [UIColor blackColor];
        [self addSubview:label];
        _nameLabel = label;
    }
    return _nameLabel;
}

- (UILabel *)topLabel
{
    if (!_topLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont boldSystemFontOfSize:17.0];
        label.textColor = [UIColor blackColor];
        label.text = @"分享给：";
        [self addSubview:label];
        _topLabel = label;
    }
    return _topLabel;
}

- (UIImageView *)imageV
{
    if (!_imageV) {
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.layer.masksToBounds = YES;
        imageV.layer.cornerRadius = 30;
        [self addSubview:imageV];
        _imageV = imageV;
    }
    return _imageV;
}

- (UIView *)lineView1
{
    if (!_lineView1) {
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = ICRGB(0xc4c4c4);
        [self addSubview:line];
        _lineView1 = line;
    }
    return _lineView1;
}


@end
